lib.use(['element', 'table','laydate','tablePlug'], function () {
    var table = layui.table;
    var installFinishTable;
    var probId=common.util.getParam("probId");
    //初始表格基础参数
    table.set(common.layui.table.config());

    // 渲染表格
    installFinishTable = table.render({
            elem: '#installFinishTable'
            , url: common.util.createUrl('/sale/installFinish/list')
            , height: '320px'
            ,toolbar: '#toolbar'
            ,where:{probId:probId}
            , cols: [[
                {type:'checkbox'}
               ,{type: 'numbers', title: '编号' }
                ,{  field: 'entId',  hide: true}
                ,{  field: 'id',  hide: true}
                ,{ title: '单据日期', field: 'finishDate' }
                ,{ field: 'probId',  hide: true}
                ,{ title: '预订单编码', field: 'probNo' }
                ,{  field: 'orderId',  hide: true}
                ,{ title: '销售订单编码', field: 'orderNo' }
                ,{ field: 'remark',  hide: true}
                ,{ field: 'createUserId',  hide: true}
                ,{  field: 'createUserName',  hide: true}
                ,{ field: 'createTime',  hide: true}
                ,{  field: 'updateUserId',  hide: true}
                ,{  field: 'updateUserName' ,  hide: true}
                ,{ title: '修改时间', field: 'updateTime' }
            ]]
    });

    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(installFinishTableFilter)', function(obj){
        update(obj)
    });

    //头工具栏事件
    table.on('toolbar(installFinishTableFilter)', function(obj){
        if(obj.event=='add') {
            var config = {
                busStyle:1,
                title:'添加完工单',
                content: ['sale/installFinish/installFinishForm.html?probId='+probId], //iframe的url，no代表不显示滚动条
            }
            parent.common.util.openWindow(busCommon.windowConfig(config))

        }else if(obj.event=="export"){
            var params=common.from.serializeObject('#searchFrm');
            params['pages']=installFinishTable.config.page.pages;
            params['limit']=installFinishTable.config.page.limit;
            params['probId']=probId;
            parent.parent.common.util.exportWindows('/sale/installFinish/export',params);
        }else if(obj.event=='del'){
            del(table.checkStatus('installFinishTable'))
        }else if(obj.event=='refresh'){
            searchTable();
        }
    })
    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        params=common.from.serializeObject('#searchFrm');
        installFinishTable.reload({where: params})
    }
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if (!common.auth.hasPermission("sale:installFinish:update")) {
            return;
        }
        var postData = [];
        if (obj.data.constructor === Array) {
            postData = obj.data.map(function (item) {
                return item['id'];
            });
        } else {
            postData = [obj.data.id]
        }
        if (postData.length != 1) return;
        var config = {
            busStyle:1,
            title:'修改完工单',
            content: 'sale/installFinish/installFinishForm.html?id=' + postData[0] + "&probId=" + probId
        }
        parent.common.util.openWindow(busCommon.windowConfig(config))
    }

    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("sale:installFinish:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){top.common.util.error('请选择一条记录');return;}
        if(obj.data[0].deliverState==2){top.common.util.error('当前发货单已审核,,禁止删除');return;}
        top.layer.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.post('/sale/installFinish/delete', postData, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
            },function(resp){
                top.layer.close(index);
                top.common.util.error(resp.msg)
            })

        });
    }

    window.formCallBack=function () {
        searchTable();
    }
});
